package com.pojoin.cms.face;

import java.util.HashSet;
import java.util.Set;

import com.pojoin.cms.dao.ResourceDao;
import com.pojoin.cms.dao.RoleDao;
import com.pojoin.cms.domain.Resource;
import com.pojoin.cms.domain.Role;
import com.pojoin.cms.domain.User;
import com.pojoin.cms.domain.alter.RoleAlter;
import com.pojoin.cms.domain.form.RoleForm;
import com.pojoin.cms.exception.CmsException;
import com.pojoin.cms.util.SpringContext;

public class RoleFaceLocal extends RoleAlter implements RoleFace {
	
	private RoleDao roleDao;

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	@Override
	public boolean isPersistenceAvailable() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void delete() throws CmsException {
		UserFaceFactory uff = (UserFaceFactory) SpringContext.getBean("userFaceFactory");
		Set<User> userSet = this.getUsers();
		for(User u : userSet){
			UserFace uf = uff.getUserFaceByUserId(u.getUserId());
			String roleIds = "";
			Set<Role> roleSet = u.getRoles();
			for (Role r : roleSet) {
				if (!r.getRoleId().toString().equals(this.roleId.toString())) {
					roleIds += r.getRoleId() + ",";
				}
			}
			uf.resetRoles(roleIds);
			uf.flush();
			roleIds = null;
			roleSet = null;
			uf = null;
		}
		this.roleDao.delRole(this.roleId);
		uff = null;
		userSet = null;

	}

	@Override
	public void flush() throws CmsException {
		this.roleDao.updateRole(this);

	}

	@Override
	public void reload() throws CmsException {
		// TODO Auto-generated method stub

	}

	@Override
	public void addRefUser(User user) {
		this.getUsers().add(user);
		
	}

	/**
	 * 初始化
	 */
	public void init() {
		Role role = this.roleDao.findRoleById(this.roleId);
		this.setCreateDate(role.getCreateDate());
		this.setDeleted(role.getDeleted());
		this.setRemark(role.getRemark());
		this.setResources(role.getResources());
		this.setRoleName(role.getRoleName());
		this.setRoleTitle(role.getRoleTitle());
		this.setUsers(role.getUsers());
	}
	
	public void setRoleId(Long roleId){
		this.roleId = roleId;
	}

	@Override
	public void resetResources(String selectedResourcesIds) {
		this.getResources().clear();
		this.setResources(this.initResourcesByIds(selectedResourcesIds));
		
	}

	/**
	 * 根据id初始化对象
	 * @param selectedResourcesIds
	 * @return
	 */
	private Set<Resource> initResourcesByIds(String selectedResourcesIds) {
		ResourceDao resourceDao = (ResourceDao) SpringContext.getBean("resourceDao");
		Set<Resource> rcset = new HashSet<Resource>();
		if(selectedResourcesIds != null && !"".equals(selectedResourcesIds)){
			String[] rIds = selectedResourcesIds.split(",");
			for(String id : rIds){
				if(id != null && !"".equals(id)){
					rcset.add(resourceDao.findById(Long.parseLong(id)));
				}
			}
		}
		return rcset;
	}

	@Override
	public void update(RoleForm role) {
		this.setRemark(role.getRemark());
		this.setRoleName(role.getRoleName());
		this.setRoleTitle(role.getRoleTitle());
		
		this.getResources().clear();
		this.setResources(this.initResourcesByIds(role.getSelectedResourcesIds()));
	}

}
